﻿using Game.Assets.Scripts.Manager;
using Game.Assets.Scripts.Model;
using QFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Game.System
{
    public class NetworkSystem : AbstractSystem
    {
        protected override void OnInit()
        {

        }

        List<CommodityZone> tempCommoditys = new List<CommodityZone>();

        /// <summary>
        /// 请求商品列表
        /// </summary>
        public void RequestCommodityList()
        {
            Debug.Log("请求所有商品");
            /*临时代码*/
            List<Commodity> tempCommodity = new List<Commodity>();
            for (int i = 0; i < APP.Interface.GetModel<TableManager>().Tables.TbShop.DataList.Count; i++)
            {
                Commodity commodity = new Commodity();
                commodity.ShopID = APP.Interface.GetModel<TableManager>().Tables.TbShop.DataList[i].ID;
                commodity.CommodityID = APP.Interface.GetModel<TableManager>().Tables.TbShop.DataList[i].CommodityID;
                commodity.Price.Value = int.Parse(APP.Interface.GetModel<TableManager>().Tables.TbShop.DataList[i].CommodityPrice);
                commodity.Amount.SetValueWithoutEvent(APP.Interface.GetModel<TableManager>().Tables.TbShop.DataList[i].CommoditySellNum);
                tempCommodity.Add(commodity);
            }
            tempCommoditys = new List<CommodityZone>();
            for (int i = 0; i < APP.Interface.GetModel<TableManager>().Tables.TbCommodityType.DataList.Count; i++)
            {
                CommodityZone zone = new CommodityZone();
                zone.Type = APP.Interface.GetModel<TableManager>().Tables.TbCommodityType.DataList[i].Type;
                zone.ResidueCD.Value = UnityEngine.Random.Range(2000, 8640);
                for (int j = 0; j < tempCommodity.Count;)
                {
                    if (APP.Interface.GetModel<TableManager>().Tables.TbShop.Get(tempCommodity[j].ShopID).CommodityType == zone.Type)
                    {
                        zone.Commoditys.Add(tempCommodity[j]);
                        tempCommodity.RemoveAt(j);
                    }
                    else
                    {
                        j++;
                    }
                }
                tempCommoditys.Add(zone);
            }


            this.GetModel<CommoditysModel>().CommodityZones.Value = tempCommoditys;
        }
        /// <summary>
        /// 请求购买商品
        /// </summary>
        /// <param name="id"></param>
        public void RequestBuyCommodity(int id)
        {
            Debug.Log(string.Format("购买ID为{0}的商品", id));
            for (int i = 0; i < tempCommoditys.Count; i++)
            {
                for (int j = 0; j < tempCommoditys[i].Commoditys.Count; j++)
                {
                    if (tempCommoditys[i].Commoditys[j].ShopID == id && tempCommoditys[i].Commoditys[j].Amount.Value > 0)
                    {
                        tempCommoditys[i].Commoditys[j].Amount.Value--;
                        if (APP.Interface.GetModel<TableManager>().Tables.TbCommodityType.Get(tempCommoditys[i].Type).IsRiseInPrice)
                        {
                            tempCommoditys[i].Commoditys[j].Price.Value += 100;
                        }
                        TypeEventSystem.Global.Send(new BuyCommodityEvent() { pieceID = tempCommoditys[i].Commoditys[j].CommodityID });
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 自动刷新商品区域
        /// </summary>
        /// <param name="type"></param>
        public void RequestAutoRefreshShopZone(int type)
        {
            Debug.Log(string.Format("自动刷新商品：" + type));
            for (int i = 0; i < tempCommoditys.Count; i++)
            {
                if (type == tempCommoditys[i].Type)
                {
                    for (int j = 0; j < tempCommoditys[i].Commoditys.Count; j++)
                    {
                        tempCommoditys[i].Commoditys[j].Amount.Value = APP.Interface.GetModel<TableManager>().Tables.TbShop.Get(tempCommoditys[i].Commoditys[j].ShopID).CommoditySellNum;
                        tempCommoditys[i].ResidueCD.Value = UnityEngine.Random.Range(2000, 8640);
                    }
                    break;
                }
            }
        }
        /// <summary>
        /// 手动刷新商品区域
        /// </summary>
        /// <param name="type"></param>
        public void RequestManualRefreshShopZone(int type)
        {
            Debug.Log(string.Format("手动刷新商品：" + type));
            for (int i = 0; i < tempCommoditys.Count; i++)
            {
                if (type == tempCommoditys[i].Type)
                {
                    for (int j = 0; j < tempCommoditys[i].Commoditys.Count; j++)
                    {
                        tempCommoditys[i].Commoditys[j].Amount.Value = APP.Interface.GetModel<TableManager>().Tables.TbShop.Get(tempCommoditys[i].Commoditys[j].ShopID).CommoditySellNum;
                    }
                    break;
                }
            }
        }
    }
    

}
